package chances.wechat.service.rdb;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import chances.wechat.data.PageBean;
import chances.wechat.entity.UserMedia;
import chances.wechat.service.UserMediaService;

@Service("userMediaService")
public class RdbUserMediaService extends BaseDao implements UserMediaService {
	
	@Value("${wechat.user.photo.url}")
	private String photoUrl;

	@Override
	public void insertMedia(UserMedia media) {
		Map<String, Object> paramsMap = new HashMap<String, Object>();
		paramsMap.put("obj", media);
		try {
			this.insert("insertMedia", paramsMap);
		} finally {
			paramsMap.clear();
			paramsMap = null;
		}

	}

	@Override
	public UserMedia getMediaByMediaId(String mediaId) {
		Map<String, Object> paramsMap = new HashMap<String, Object>();
		paramsMap.put("mediaId", mediaId);
		try {
			return (UserMedia) this.queryForObject("getMediaByMediaId", paramsMap);
		} finally {
			paramsMap.clear();
			paramsMap = null;
		}
	}

	@Override
	public void updateMedia(UserMedia media) {
		Map<String, Object> paramsMap = new HashMap<String, Object>();
		paramsMap.put("obj", media);
		try {
			this.update("updateMedia", paramsMap);
		} finally {
			paramsMap.clear();
			paramsMap = null;
		}
	}

	@Override
	public void delMediaByMediaIds(String[] mediaIds, Integer status) {
		Map<String, Object> paramsMap = new HashMap<String, Object>();
		paramsMap.put("status", status);
		paramsMap.put("mediaIds", mediaIds);
		try {
			this.update("delMediaByMediaIds", paramsMap);
		} finally {
			paramsMap.clear();
			paramsMap = null;
		}
		
	}

	@Override
	public String getPhotoUrl() {
		return photoUrl;
	}

	@Override
	public void setPhotoUrl(String photoUrl) {
		this.photoUrl = photoUrl;
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Object> listUserMedia(UserMedia media, int begin, PageBean pageBean) {
		Map<String, Object> paramsMap = new HashMap<String, Object>();
		paramsMap.put("obj", media);
		try {
			return (List<Object>) this.queryForList("listStbMediaByOpenIdAndMediaStatus", paramsMap, begin, pageBean);
		} finally {
			paramsMap.clear();
			paramsMap = null;
		}
		
	}

	@Override
	@SuppressWarnings("unchecked")
	public int getTotalCount(UserMedia userMediaParam) {
		Map<String, Object> paramsMap = new HashMap<String, Object>();
		paramsMap.put("obj", userMediaParam);
		try {
			List<Object> result = (List<Object>) this.queryForList("listStbMediaByOpenIdAndMediaStatus", paramsMap);
			return result.size();
		} finally {
			paramsMap.clear();
			paramsMap = null;
		}
	}

}
